<template>
  <!-- Teleport 将内容传送到 body 下，方便控制层级 -->
  <Teleport to="body">
    <div
        v-if="isShow"
        class="cart-popup-mask"
        @click="closePopup"
    >
      <div class="cart-popup">
        <div class="popup-header">
          <h3>购物车</h3>
          <button @click="clean">清空购物车</button>
          <button @click="closePopup">关闭</button>
        </div>
        <div class="popup-body">
          <ul>
            <li v-for="item in cartItems" :key="item.id">
              <img :src="item.image" style="width: 100px; height: 100px;" />
              <div class="item-info">
                <span>{{ item.name }}</span>
                <span>¥{{ item.amount }}</span>
              </div>
              <div class="item-count">
                <button @click="decreaseCount(item.dishId)" style="background-image: url('@/static/btn_red.png')">-</button>
                <span>{{ item.number }}</span>
                <button @click="increaseCount(item.dishId)" style="background-image: url('@/static/add.png')">+</button>
              </div>
            </li>
          </ul>
        </div>
        <div class="popup-footer">
          <span>总计：¥{{ totalPrice }}</span>
          <RouterLink to="/order" style="text-decoration: none; color: #333;" >去结算</RouterLink>
          <button @click="toSettle">去结算</button>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { ref, computed } from 'vue';

// 接收父组件传递的购物车数据、弹窗显示状态等
const props = defineProps({
  cartItems: {
    type: Array,
    default: () => [],
  },
  isShow: {
    type: Boolean,
    default: false,
  },
});

// 向父组件传递事件
const emit = defineEmits(['close', 'changeCount', 'settle','clean']);

// 清空购物车
const clean = () => {
  emit('clean');
};
// 关闭弹窗
const closePopup = () => {
  emit('close');
};

// 增加商品数量
const increaseCount = (id) => {
  emit('changeCount', { id, type: 'increase' });
};

// 减少商品数量
const decreaseCount = (id) => {
  emit('changeCount', { id, type: 'decrease' });
};

// 去结算
const toSettle = () => {
  emit('settle');
};

// 计算总价
const totalPrice = computed(() => {
  return props.cartItems.reduce((total, item) => {
    return total + item.amount * item.number;
  }, 0);
});
</script>

<style scoped>
/* 遮罩层样式 */
.cart-popup-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: flex-end;
  z-index: 999;
}

/* 弹窗样式 */
.cart-popup {
  width: 100%;
  max-width: 500px;
  background-color: #fff;
  border-radius: 8px 8px 0 0;
  animation: slideUp 0.3s ease-in-out;
}

/* 弹窗头部样式 */
.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #eee;
}

.popup-header button {
  background-color: transparent;
  border: none;
  font-size: 16px;
  cursor: pointer;
}

/* 弹窗主体样式 */
.popup-body {
  max-height: 300px;
  overflow-y: auto;
  padding: 12px 16px;
}

.popup-body ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.popup-body li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.item-info {
  display: flex;
  flex-direction: column;
}

.item-count {
  display: flex;
  align-items: center;
}

.item-count button {
  width: 24px;
  height: 24px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: #fff;
  cursor: pointer;
}

/* 弹窗底部样式 */
.popup-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-top: 1px solid #eee;
}

.popup-footer button {
  background-color: #f00;
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

/* 弹窗滑入动画 */
@keyframes slideUp {
  from {
    /* 起始位置：元素自身宽度的左侧，高度的下方 */
    transform: translateY(100%);
    opacity: 0; /* 可选：添加淡入效果 */
  }
  to {
    /* 结束位置：元素原始位置 */
    transform: translatey(0);
    opacity: 1; /* 可选：添加淡入效果 */
  }
}
</style>